package baidu.reader.txt.ui
{
    import baidu.reader.common.*;
    import baidu.reader.common.effects.*;
    import baidu.reader.txt.model.*;
    import baidu.ui.constants.*;
    import baidu.ui.containers.*;
    import baidu.ui.layout.*;
    import baidu.ui.managers.*;
    
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;

    public class TxtSingleBookPage extends TxtBookPageBase
    {
		[Embed(source="assets/sprite39.swf")]
		private var LeftCursor:Class;
		
		[Embed(source="assets/sprite56.swf")]
		private var RightCursor:Class;
		
		[Embed(source="assets/sprite27.swf")]
		private var ClickArea:Class;
		
		[Embed(source="assets/sprite35.swf")]
		private var PageBG:Class;
        private var pageBG:Sprite;
        private var pageContent:ScrollPane;
        private var _textfield:TextField;
        private var textContainer:Sprite;
        private var textFormat:TextFormat;
        private var previousScrollTopPosition:Number = 0;
        private var previousScrollBottomPosition:Number = 0;
        private var screenFirstCharIndex:int;
        private var _updatePending:Boolean = false;
        private var _needUpdate:Boolean = false;

        public function TxtSingleBookPage()
        {
            this._textfield = new TextField();
            this.textContainer = new Sprite();
            return;
        }

        override protected function addPageBG() : void
        {
            this.pageBG = new PageBG();
            addChild(this.pageBG);
            var _loc_1:* = this._childsLayoutInfo[this.pageBG] as LayoutInfo;
            _loc_1.widthPercent = 100;
            _loc_1.heightPercent = 100;
            return;
        }

        override protected function addLeftClickArea() : void
        {
            if (!leftClickPane)
            {
                leftClickPane = new ClickArea();
                leftClickPane.alpha = 0;
            }
            addChild(leftClickPane);
            var _loc_1:* = _childsLayoutInfo[leftClickPane];
            _loc_1.left = 0;
            _loc_1.top = 20;
            _loc_1.bottom = 70;
            _loc_1.widthPercent = 25;
            CursorManager.getInstance().register(leftClickPane, LeftCursor);
            leftClickPane.addEventListener(MouseEvent.CLICK, this.previousClickHandler);
            return;
        }

        override protected function addRightClickArea() : void
        {
            if (!rightClickPane)
            {
                rightClickPane = new ClickArea();
                rightClickPane.alpha = 0;
            }
            addChild(rightClickPane);
            var _loc_1:* = _childsLayoutInfo[rightClickPane];
            _loc_1.top = 20;
            _loc_1.bottom = 70;
            _loc_1.offsetCenterH = 0;
            _loc_1.right = 55;
            CursorManager.getInstance().register(rightClickPane, RightCursor);
            rightClickPane.addEventListener(MouseEvent.CLICK, this.nextClickHandler);
            return;
        }

        override protected function addPageContent() : void
        {
            var _loc_1:Number = NaN;
            stage.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_UP));
            if (this.updatePending)
            {
                return;
            }
            this.initTextfield(this._textfield);
            switch(chapterModel.textStrategy)
            {
                case TxtChapterModel.BOTTOM:
                {
                    this._textfield.appendText(chapterModel.currentChapterText);
                    _loc_1 = this.previousScrollTopPosition;
                    break;
                }
                case TxtChapterModel.TOP:
                {
                    this._textfield.text = chapterModel.currentChapterText + this._textfield.text;
                    break;
                }
                case TxtChapterModel.REPLACE:
                {
                    this._textfield.text = chapterModel.currentChapterText;
                    _loc_1 = 0;
                    break;
                }
                default:
                {
                    break;
                }
            }
            while (this.textContainer.numChildren)
            {
                
                this.textContainer.removeChildAt(0);
            }
            this.textContainer.visible = true;
            this.textContainer.addChild(this._textfield);
            if (this.pageContent && this.contains(this.pageContent))
            {
                this.removeChild(this.pageContent);
            }
            this.pageContent = new ScrollPane();
            this.pageContent.cacheAsBitmap = true;
            this.pageContent.vScrollBar.setStyle("skin", ResourceFactory.AppScrollBar_Skin);
            this.pageContent.hScrollBar.setStyle("skin", ResourceFactory.AppScrollBar_Skin);
            this.pageContent.vScrollPolicy = ScrollPolicy.ON;
            this.pageContent.hScrollPolicy = ScrollPolicy.OFF;
            this.pageContent.addEventListener(Event.CHANGE, this.scrollChangeHandler);
            addChild(this.pageContent);
            this.layoutPageContent();
            this.pageContent.content = this.textContainer;
            if (chapterModel.textStrategy == TxtChapterModel.TOP)
            {
                _loc_1 = this.pageContent.content.height - this.previousScrollBottomPosition;
            }
            if (!isNaN(_loc_1))
            {
                this.pageContent.vScrollPosition = _loc_1;
            }
            this.resizePane();
            this.restorePosition();
            addClickArea();
            return;
        }

        private function scrollChangeHandler(event:Event = null) : void
        {
            var _loc_2:* = (this.pageContent.vScrollPosition + this.pageContent.availableHeight) / this.pageContent.contentHeight;
            this.previousScrollTopPosition = this.pageContent.vScrollPosition;
            this.previousScrollBottomPosition = this.pageContent.contentHeight - this.pageContent.vScrollPosition;
            trace("percent:" + _loc_2);
            if (_loc_2 < 0.25 && this.pageContent.vScrollPosition != 0)
            {
                dispatchEvent(new ReaderEvent(ReaderEvent.SCROLLED_TOP));
            }
            else if (_loc_2 >= (this.pageContent.maxVScrollPosition - this.pageContent.availableHeight) / this.pageContent.maxVScrollPosition)
            {
                dispatchEvent(new ReaderEvent(ReaderEvent.SCROLLED_BOTTOM));
            }
            dispatchEvent(new ReaderEvent(ReaderEvent.SCROLL, _loc_2));
            return;
        }

        override public function getCurrentFirstCharIndex() : int
        {
            if (!this.pageContent)
            {
                return 0;
            }
            var _loc_1:* = this.pageContent.vScrollPosition + 2;
            while (this._textfield.getCharIndexAtPoint(2, _loc_1) == -1 && _loc_1 < this.pageContent.availableHeight + this.pageContent.vScrollPosition)
            {
                
                _loc_1 = _loc_1 + 2;
            }
            var _loc_2:* = this._textfield.getCharIndexAtPoint(2, _loc_1);
            return _loc_2;
        }

        override public function getSecondLineFirstCharIndex() : int
        {
            if (!this.pageContent)
            {
                return 0;
            }
            var _loc_1:* = this.pageContent.vScrollPosition + this._textfield.getLineMetrics(0).height + 2;
            while (this._textfield.getCharIndexAtPoint(2, _loc_1) == -1 && _loc_1 < this.pageContent.availableHeight + this.pageContent.vScrollPosition)
            {
                
                _loc_1 = _loc_1 + 2;
            }
            var _loc_2:* = this._textfield.getCharIndexAtPoint(2, _loc_1);
            return _loc_2;
        }

        private function resizePane() : void
        {
            if (!this.pageContent)
            {
                return;
            }
            var _loc_1:* = this._textfield.getLineMetrics(0).height;
            var _loc_2:* = Math.round(this.pageContent.height / _loc_1);
            var _loc_3:* = _loc_2 * _loc_1;
            if (_loc_3 > this.height - 60)
            {
                _loc_3 = _loc_3 - _loc_1;
            }
            this.pageContent.height = _loc_3;
            this.pageContent.vLineScrollSize = _loc_1;
            nextChapterButton.drawNow();
            this.textContainer.addChild(nextChapterButton);
            var _loc_4:* = (int((this._textfield.height + nextChapterButton.height) / _loc_3) + 1) * _loc_3 - this._textfield.height - nextChapterButton.height;
            if ((int((this._textfield.height + nextChapterButton.height) / _loc_3) + 1) * _loc_3 - this._textfield.height - nextChapterButton.height >= _loc_3 - nextChapterButton.height - _loc_1)
            {
                _loc_4 = _loc_1;
            }
            nextChapterButton.y = _loc_4 + this._textfield.height;
            nextChapterButton.x = (this.pageContent.availableWidth - nextChapterButton.width) / 2;
            if (!chapterModel.chapterNotEnd && chapterModel.currentChapterIndex < (chapterModel.totalChapter - 1))
            {
                nextChapterButton.visible = true;
            }
            else
            {
                nextChapterButton.visible = false;
            }
            this.pageContent.update();
            return;
        }

        private function initTextfield(param1:TextField) : void
        {
            this.textFormat = new TextFormat(preferenceModel.fontName, preferenceModel.fontSize);
            this.textFormat.leading = preferenceModel.leading;
            param1.wordWrap = true;
            param1.defaultTextFormat = this.textFormat;
            param1.setTextFormat(this.textFormat, -1, (param1.text.length - 1));
            param1.width = screenModel.stageWidth - 120;
            param1.selectable = false;
            param1.autoSize = TextFieldAutoSize.LEFT;
            param1.cacheAsBitmap = true;
            return;
        }

        override protected function screenResizeHandler(event:Event) : void
        {
            this.screenFirstCharIndex = this.getCurrentFirstCharIndex();
            this.initTextfield(this._textfield);
            this.layoutPageContent();
            this.resizePane();
            this.scrollToCharIndex(this.screenFirstCharIndex);
            return;
        }

        private function scrollToCharIndex(param1:int) : void
        {
            var destY1:Number;
            var charIndex:* = param1;
            if (chapterModel.useLastPage)
            {
                this.pageContent.vScrollPosition = this.pageContent.maxVScrollPosition;
                return;
            }
            if (charIndex < 0 || charIndex >= this._textfield.text.length)
            {
                return;
            }
            try
            {
                destY1 = this._textfield.getLineMetrics(0).height * this._textfield.getLineIndexOfChar(charIndex);
                this.pageContent.vScrollPosition = destY1 > this.pageContent.contentHeight ? (this.pageContent.contentHeight) : (destY1);
            }
            catch (e:Error)
            {
                trace(e.getStackTrace());
            }
            return;
        }

        private function layoutPageContent() : void
        {
            if (!this.pageContent)
            {
                return;
            }
            this.pageContent.y = 45;
            this.pageContent.x = 50;
            this.pageContent.width = screenModel.stageWidth - 100;
            this.pageContent.height = screenModel.stageHeight - 135;
            return;
        }

        private function nextClickHandler(event:MouseEvent) : void
        {
            dispatchEvent(new ReaderEvent(ReaderEvent.GO_NEXT_PAGE));
            return;
        }

        private function previousClickHandler(event:MouseEvent) : void
        {
            dispatchEvent(new ReaderEvent(ReaderEvent.GO_PREV_PAGE));
            return;
        }

        override public function goNextPage() : void
        {
            this.doDisplayNextPage();
            var _loc_1:Number = 20;
            if (this.pageContent.maxVScrollPosition - this.pageContent.vScrollPosition <= this.pageContent.vPageScrollSize + _loc_1)
            {
            }
            this.pageContent.vScrollPosition = this.pageContent.vScrollPosition + this.pageContent.vPageScrollSize;
            return;
        }

        override public function goPreviousPage() : void
        {
            var _loc_1:Number = NaN;
            if (this.pageContent.vScrollPosition < this.pageContent.vPageScrollSize)
            {
                _loc_1 = this.pageContent.vScrollPosition;
            }
            else
            {
                _loc_1 = this.pageContent.vPageScrollSize;
            }
            this.pageContent.vScrollPosition = this.pageContent.vScrollPosition - _loc_1;
            this.doDisplayPrevPage();
            this.pageContent.vScrollPosition = this.pageContent.vScrollPosition + _loc_1;
            this.pageContent.drawNow();
            return;
        }

        override public function goDown() : void
        {
            this.pageContent.vScrollPosition = this.pageContent.vScrollPosition + this.pageContent.vLineScrollSize;
            return;
        }

        override public function goUp() : void
        {
            this.pageContent.vScrollPosition = this.pageContent.vScrollPosition - this.pageContent.vLineScrollSize;
            return;
        }

        private function doDisplayPrevPage() : void
        {
            PageTurnManager.getInstance().startSingleTurn(this, false);
            PageTurnManager.getInstance().addEventListener(PageEffectEvent.EFFECT_END, this.onPrevEffectEnd);
            return;
        }

        private function onPrevEffectEnd(event:PageEffectEvent) : void
        {
            event.target.removeEventListener(PageEffectEvent.EFFECT_END, this.onPrevEffectEnd);
            if (!this.updatePending)
            {
                this.pageContent.vScrollPosition = this.pageContent.vScrollPosition - this.pageContent.vPageScrollSize;
            }
            else
            {
                this.updatePending = false;
                this.addPageContent();
            }
            return;
        }

        private function doDisplayNextPage() : void
        {
            PageTurnManager.getInstance().startSingleTurn(this, true);
            return;
        }

        override public function setTextFormat(param1:TextFormat) : void
        {
            this.screenFirstCharIndex = this.getCurrentFirstCharIndex();
            this._textfield.setTextFormat(param1, 0, (this._textfield.text.length - 1));
            this._textfield.defaultTextFormat = param1;
            this.addPageContent();
            this.scrollToCharIndex(this.screenFirstCharIndex);
            return;
        }

        public function restorePosition() : void
        {
            var _loc_1:* = chapterModel.destCharIndex;
            if (_loc_1 == -2)
            {
                this.pageContent.vScrollPosition = this.pageContent.maxVScrollPosition;
            }
            else if (_loc_1 != -1)
            {
                this.scrollToCharIndex(_loc_1);
            }
            return;
        }

        override public function get scrollPercent() : Number
        {
            var _loc_1:Number = NaN;
            if (!this.pageContent.content)
            {
                _loc_1 = 0;
            }
            _loc_1 = this.pageContent.vScrollPosition / this.pageContent.maxVScrollPosition * 100;
            return _loc_1;
        }

        public function get updatePending() : Boolean
        {
            return this._updatePending;
        }

        public function set updatePending(param1:Boolean) : void
        {
            this._updatePending = param1;
            return;
        }

        private function get needUpdate() : Boolean
        {
            return this._needUpdate;
        }

        private function set needUpdate(param1:Boolean) : void
        {
            this._needUpdate = param1;
            return;
        }

    }
}
